import Vue from "vue";
import VueRouter from "vue-router";
import routes from './routes'
import {
   isLogin
} from "../Utils/auth"
//注册路由中间件
Vue.use(VueRouter)


// 定义一个路由实例，并且提供了这个路由实例的模式和路由定义
let router = new VueRouter({
   mode: "history",
   routes
})

//q全局前置守卫
//设置每个页面的标题
router.beforeEach((to, from, next) => {
   console.log(to);
   document.title = to.meta.title;

   //判断是否有currentUser
   var u = isLogin()
  
   if (to.path == '/login') {
      if (u) {
         alert('你已经登录过了')
         next('dashboard')
      } else {
         next()
      }
   } else {
      if (u) {
         next()
      } else {
         alert('你没有登陆哦');
         //跳到指定路由
         next('login')
      }
   }
})

export default router